﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Globalization;
using System.Resources;
using System.Threading;
using Telerik.WinControls.UI;

namespace CRMG.Container.Views.Training
{
    public partial class FrmAvailableTrain : RadForm
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        TrainManager trainMan;
        LookupManager lookupMan;
        AvailableTrain pAvTrain;
        int pAvTrainId = -1;
        DateTime dt;

        public FrmAvailableTrain()
            : this(-1)
        { }

        public FrmAvailableTrain(int AvailableTrainId)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                InitializeComponent();
                pAvTrainId = AvailableTrainId;
                pAvTrain = new AvailableTrain();
                trainMan = new TrainManager();
                lookupMan = new LookupManager();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmAvailableTrain).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmAvailableTrain).Assembly);
                lblTrain.Text = formRes.GetString("FrmAvailableTrain.lblTrain");
                lblTrainClass.Text = formRes.GetString("FrmAvailableTrain.lblTrainClass");
                lblUser.Text = formRes.GetString("FrmAvailableTrain.lblUser");
                lblTrainAmount.Text = formRes.GetString("FrmAvailableTrain.lblTrainAmount");
                lblStartTime.Text = formRes.GetString("FrmAvailableTrain.lblStartTime");
                lblEndTime.Text = formRes.GetString("FrmAvailableTrain.lblEndTime");
                lblCourseType.Text = formRes.GetString("FrmAvailableTrain.lblCourseType");
                btnSave.Text = AvailableTrainId == -1 ? formRes.GetString("Add") : formRes.GetString("Update");
                btnCancel.Text = formRes.GetString("Cancel");
                dtpStartTime.Culture = new CultureInfo(AppVariables.ApplicationCulture);
                dtpEndTime.Culture = new CultureInfo(AppVariables.ApplicationCulture);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2033,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2033)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmAvailableTrain.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                SaveEnable(false);
            }
        }


        private void formLoad(object sender, EventArgs e)
        {
            try
            {
                ControlUtil.LoadDataTable2DropDownList(drpTrain, trainMan.GetTrains(), "TrainName", "OBJID");
                ControlUtil.LoadDataTable2DropDownList(drpTrainClass, trainMan.GetTrainClasses(), "TrainClassName", "OBJID");
                ControlUtil.LoadDataTable2DropDownList(drpUser, trainMan.GetTrainers(), "Name", "OBJID");
                ControlUtil.LoadDataTable2DropDownList(drpCourseType, lookupMan.GetCourseTypes(), "CourseTypeName", "OBJID");

                if (pAvTrainId != -1)
                {
                    pAvTrain = trainMan.GetAvailableTrainById(pAvTrainId);
                    ControlUtil.SetSelectedValueOfDropDown(drpTrain, pAvTrain.TrainId);
                    ControlUtil.SetSelectedValueOfDropDown(drpTrainClass, pAvTrain.TrainClassId);
                    ControlUtil.SetSelectedValueOfDropDown(drpUser, pAvTrain.UserId);
                    txtAmount.NumericValue = pAvTrain.TrainAmount;
                    dtpStartTime.Value = pAvTrain.StartTime;
                    dtpEndTime.Value = pAvTrain.EndTime;
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmAvailableTrain.Load.Error"),
                      messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2034,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2034)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmAvailableTrain.Load.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                pAvTrain = new AvailableTrain();
                SaveEnable(false);
                if (pAvTrainId != -1)
                    pAvTrain = new AvailableTrain();
            }
        }

        private void TrainChange(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            pAvTrain.TrainId = drpTrain.SelectedValue.ToInt();
            SaveEnable(true);
        }

        private void TrainClassChange(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            pAvTrain.TrainClassId = drpTrainClass.SelectedValue.ToInt();
            SaveEnable(true);
        }

        private void UserChange(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            pAvTrain.UserId = drpUser.SelectedValue.ToInt();
            SaveEnable(true);
        }

        private void AmountChange(object sender, EventArgs e)
        {
            pAvTrain.TrainAmount = txtAmount.NumericValue;
            SaveEnable(true);
        }

        private void StartTimeChange(object sender, EventArgs e)
        {
            pAvTrain.StartTime = dtpStartTime.Value;
            SaveEnable(true);
        }

        private void EndTimeChange(object sender, EventArgs e)
        {
            pAvTrain.EndTime = dtpEndTime.Value;
            SaveEnable(true);
        }

        private void CourseTypeChange(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            pAvTrain.CourseType = drpCourseType.SelectedValue.ToInt();
            SaveEnable(true);
        }

        private void SaveEnable(bool state)
        {
            btnSave.Enabled = state;
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                if (pAvTrain.TrainId == 0)
                {
                    ControlUtil.ShowWarningMessage(
                        messageRes.GetString("FrmAvailableTrain.Save.InvalidTrain"),
                        messageRes.GetString("Warning"));
                    return;
                }

                if (pAvTrain.UserId == 0)
                {
                    ControlUtil.ShowWarningMessage(
                           messageRes.GetString("FrmAvailableTrain.Save.InvalidUser"),
                           messageRes.GetString("Warning"));
                    return;
                }
                if (pAvTrain.TrainClassId == 0)
                {
                    ControlUtil.ShowWarningMessage(
                           messageRes.GetString("FrmAvailableTrain.Save.InvalidTrainClass"),
                           messageRes.GetString("Warning"));
                    return;
                }

                if (pAvTrain.TrainAmount <= 0)
                {
                    ControlUtil.ShowWarningMessage(
                           messageRes.GetString("FrmAvailableTrain.Save.InvalidTrainAmount"),
                           messageRes.GetString("Warning"));
                    return;
                }

                if (pAvTrain.CourseType == 0)
                {
                    ControlUtil.ShowWarningMessage(
                               messageRes.GetString("FrmAvailableTrain.Save.InvalidCourseType"),
                               messageRes.GetString("Warning"));
                    return;
                }

                if (dt.Equals(pAvTrain.StartTime) == true)
                {
                    ControlUtil.ShowWarningMessage(
                           messageRes.GetString("FrmAvailableTrain.Save.InvalidStartTime"),
                           messageRes.GetString("Warning"));
                    return;
                }

                if (dt.Equals(pAvTrain.EndTime) == true)
                {
                    ControlUtil.ShowWarningMessage(
                           messageRes.GetString("FrmAvailableTrain.Save.InvalidEndTime"),
                           messageRes.GetString("Warning"));
                    return;
                }

                if (pAvTrain.StartTime.CompareTo(pAvTrain.EndTime) > 0)
                {
                    ControlUtil.ShowWarningMessage(
                          messageRes.GetString("FrmAvailableTrain.Save.GreaterStartTime"),
                          messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag.ToStr()
                    };
                    if (pAvTrainId == -1)
                    {
                        int Id = trainMan.AddAvailableTrain(pAvTrain);
                        transLogger.LogObject = Id;
                        transLogger.TransactionType = TransactionTypes.Insert;
                    }
                    else
                    {
                        if (pAvTrain.GetColumnChangeList().Count > 1)
                        {
                            trainMan.UpdateAvailableTrain(pAvTrain);
                            transLogger.LogObject = pAvTrainId;
                            transLogger.TransactionType = TransactionTypes.Update;
                        }
                    }
                    transLogger.Insert();
                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                        messageRes.GetString("FrmAvailableTrain.Save.Success"),
                        messageRes.GetString("Info"));
                }

            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmAvailableTrain.Save.Error"),
                      messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2035,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2035)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmAvailableTrain.Save.Error"),
                      messageRes.GetString("Error"));
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            Close();
        }

    }
}
